package org.jdsm.flow.gxt.users.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.button.ToggleButton;
import com.sencha.gxt.widget.core.client.container.CardLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.info.Info;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;
import org.jdsm.flow.gxt.users.client.widgets.user.UserGroupGrid;
import org.jdsm.flow.gxt.users.client.widgets.user.UserRoleGrid;
import org.jdsm.flow.gxt.users.client.widgets.user.UsersGrid;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 10/1/12
 * Time: 12:02 PM
 */
public class UserLayout implements IsWidget{

    @UiField
    ToggleButton usersBtn, rolesBtn;
    @UiField
    CardLayoutContainer layout;

    @UiField
    protected UsersGrid userGrid;
    @UiField
    protected UserRoleGrid userRoleGrid;
    @UiField
    protected UserGroupGrid userGroupGrid;
    @UiField
    protected PagingToolBar userToolBar;

    @UiField
    protected Window window;
    @UiField
    protected TextButton editUserBtn;

    private UserAutoBeanFactory.User selectedUser;



    @Override
    public Widget asWidget() {
        Widget rootElement = ourUiBinder.createAndBindUi(this);

        final UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);

        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.add(usersBtn);
        toggleGroup.add(rolesBtn);

        userGrid.getLoader().setLimit(10);
        userToolBar.bind(userGrid.getLoader());
        userToolBar.setPageSize(10);

//        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);
        IdentityValueProvider<UserAutoBeanFactory.User> identityValueProvider =
                new IdentityValueProvider<UserAutoBeanFactory.User>();

        final CheckBoxSelectionModel<UserAutoBeanFactory.User> selectionModel =
                new CheckBoxSelectionModel<UserAutoBeanFactory.User>(identityValueProvider);

        userGrid.getGrid().getSelectionModel().addSelectionChangedHandler(new SelectionChangedEvent.SelectionChangedHandler<UserAutoBeanFactory.User>() {
            @Override
            public void onSelectionChanged(SelectionChangedEvent<UserAutoBeanFactory.User> userSelectionChangedEvent) {
                if(userSelectionChangedEvent.getSelection().size() == 1){
                    selectedUser = userSelectionChangedEvent.getSelection().get(0);
                    editUserBtn.setEnabled(true);
                }else{
                    userGrid.getGrid().getSelectionModel().deselect(selectedUser);
                    selectedUser = userGrid.getGrid().getSelectionModel().getSelectedItem();
                }
            }
        });

/*        Menu contextMenu = new Menu();

        MenuItem editRoles = new MenuItem();
        editRoles.setText("Edit roles");
        editRoles.addSelectionHandler(new SelectionHandler<Item>() {
            @Override
            public void onSelection(SelectionEvent<Item> event) {
                Info.display("Context menu", "selected");
            }
        });

        contextMenu.add(editRoles);

        MenuItem editGroups = new MenuItem();
        editGroups.setText("Edit groups");
        editGroups.addSelectionHandler(new SelectionHandler<Item>() {
            @Override
            public void onSelection(SelectionEvent<Item> event) {

            }
        });

        contextMenu.add(editGroups);


        userGrid.getGrid().setContextMenu(contextMenu);*/

        /*userGrid.getGrid().getSelectionModel().addSelectionHandler(new SelectionHandler<UserAutoBeanFactory.User>() {
            @Override
            public void onSelection(SelectionEvent<UserAutoBeanFactory.User> userSelectionEvent) {
                Info.display("Selected item",userSelectionEvent.getSelectedItem().getName());
                UserAutoBeanFactory.PagingListLoadConfig config = userRoleGrid.getCurrentLoader().getLastLoadConfig();
                if(config == null){
                    config = factory.pagingLoadConfig().as();
                }

                UserAutoBeanFactory.Paging paging = factory.paging().as();
                StringBuilder builder = new StringBuilder("");
                builder.append("{user:{'id':'").append(userSelectionEvent.getSelectedItem().getId()).append("'}");
                paging.setFilter(builder.toString());

                config.setPaging(paging);
                userRoleGrid.getCurrentLoader().load(config);
            }
        });*/




        return rootElement;
    }

    interface UserLayoutUiBinder extends UiBinder<Widget, UserLayout> {
    }

    private static UserLayoutUiBinder ourUiBinder = GWT.create(UserLayoutUiBinder.class);

    public UserLayout() {
//        Widget rootElement = ourUiBinder.createAndBindUi(this);
    }

    @UiHandler({"editUserBtn"})
    public void showWindow(SelectEvent event){
        window.show();
        if(selectedUser == null){
            return;
        }
        UserAutoBeanFactory factory = GWT.create(UserAutoBeanFactory.class);

        UserAutoBeanFactory.PagingListLoadConfig config = userRoleGrid.getCurrentLoader().getLastLoadConfig();
        if(config == null){
            config = factory.pagingLoadConfig().as();
        }

        UserAutoBeanFactory.Paging paging = factory.paging().as();
        StringBuilder builder = new StringBuilder("");
        builder.append("{user:{'id':'").append(selectedUser.getId()).append("'}");
        paging.setFilter(builder.toString());
        config.setPaging(paging);
        userRoleGrid.getCurrentLoader().load(config);
        userGroupGrid.getCurrentLoader().load(config);


        userRoleGrid.getFullLoader().load();
        userGroupGrid.getFullLoader().load();
    }

    @UiHandler({"usersBtn"})
    public void showUserGrid(SelectEvent event){
        layout.setActiveWidget(layout.getWidget(1));
        Info.display("Info", "show event");
    }
}